
//
// 110703 - AH - Created.
//

package org.soekrat.gui.swing.textcloud;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JComponent;

import org.soekrat.gui.swing.TestFrame;

/**
 * <p>
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class SwingTextCloud extends JComponent implements TextCloud
{
  private final Set<CloudText> m_texts=new HashSet<CloudText>();
  
  private int m_lines=2;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public SwingTextCloud(){}

  // Specified in TextCloud.
  public final void add(CloudText text)
  {
    if (text==null)
    {
      throw new NullPointerException("text");  
    }
    
    m_texts.add(text);
    repaint();
  }

  // Specified in TextCloud.
  public final void remove(CloudText text) 
  {
    if (text==null)
    {
      throw new NullPointerException("text");  
    }
    
    m_texts.remove(text);
    repaint();
  }
  
  // Specified in TextCloud.
  public final Iterator<CloudText> iterator(){ return m_texts.iterator(); }
  
  // Specified in TextCloud.
  public void setLines(int n)
  {
    if (n<1)
    {
      throw new IllegalArgumentException("n<1 : "+n);
    }
    
    m_lines=n;
  }

  // Specified in TextCloud.
  public int getLines(){ return m_lines; }
  
  @Override
  protected void paintComponent(Graphics g) 
  {
    Graphics2D g2=(Graphics2D)g.create();
    
    int w=getWidth();
    int h=getHeight();
    
    for (CloudText curr : this)
    {
      String text=curr.getText();
      
      AttributedString as=new AttributedString(text);
      
      
      AttributedCharacterIterator aci=as.getIterator();
      
      
      
      g2.drawString(aci, 10, 10);
    }
  }
  
  public static void main(String[] args) 
  {
    SwingTextCloud stc=new SwingTextCloud();
    stc.add(new DefaultCloudText("allo"));
    
    TestFrame.show(stc, 300, 300);
  }
  
}
